﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vnMcdonald.Models;

namespace vnMcdonald.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /Cart/
        vnMcdonaldContext db = new vnMcdonaldContext();
        ShoppingCart cart = new ShoppingCart();
        public ActionResult Index()
        {

            ViewBag.TotalCart = cart.GetTotalPrice();
            if (Request.IsAjaxRequest())
            {
                return PartialView(cart.GetCart());
            }
            return View(cart.GetCart());
        }

        public PartialViewResult CountCart()
        {
            ViewBag.CountCart = cart.CountCart();
            return PartialView();
        }

        public ActionResult AddCart(int ProductId)
        {
            var product = db.Products.Where(p => p.ProductId == ProductId).FirstOrDefault();
            var flag = false;
            if (product != null)
            {
                if (cart.AddCart(product, 1))
                {
                    flag = true;
                }
            }
            if (Request.IsAjaxRequest())
            {
                if (flag)
                {
                    return RedirectToAction("MessageOrder", "Cart", new { Name = product.Name });
                }
            }

            return RedirectToAction("Index", "Cart");

        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddToCart(int ProductId)
        {
            var product = db.Products.Where(p => p.ProductId == ProductId).FirstOrDefault();
            int quantity = Convert.ToInt32(Request["Quantity"].ToString());
            var flag = false;
            if (product != null)
            {
                if (cart.AddCart(product, quantity))
                {
                    flag = true;
                }
            }
            if (Request.IsAjaxRequest())
            {
                if (flag)
                {
                    return RedirectToAction("MessageOrder", "Cart", new { Name = product.Name });
                }
            }
            return RedirectToAction("Index", "Cart");
        }

        public PartialViewResult MessageOrder(string Name)
        {
            ViewBag.NameProduct = Name;
            return PartialView();
        }

        [HttpPost]
        public ActionResult SaveCart(int ProductId)
        {
            var quantity = Convert.ToInt32(Request["Quantity"]);
            cart.UpdateCart(ProductId, quantity);
            if (Request.IsAjaxRequest())
            {
                var json = Json(new { Success = true});
                return json;
            }
            
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult DeleteCart(int ProductId)
        {
            cart.DeleteCart(ProductId);
            if (Request.IsAjaxRequest())
            {
                var json = Json(new { Success = true});
                return json;
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult ClearCart()
        {
            var flag = false;
            if (cart.ClearCart())
            {
                flag = true;
            }
            if (Request.IsAjaxRequest())
            {
                var json = Json(new { Success = flag });
                return json;
            }
            return RedirectToAction("Index");
        }
    }
}
